package com.vjwealth.android.nos.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.andview.refreshview.recyclerview.BaseRecyclerAdapter

/**
 * projectName NOS_Android
 * date 2017/3/10
 * description com.vjwealth.vjcarclub.activitys.news
 */
abstract class RecyclerViewXRefreshAdapter<T> : BaseRecyclerAdapter<RecyclerViewXRefreshAdapter.CommonViewHolder> {

    protected var mContext: Context
    protected var mInflater: LayoutInflater
    protected var mLayoutId: Int = 0
    protected var mArrayList: List<T>
    protected var onItemClickListener: OnItemClickListener<T>? = null
    protected var multiItemTypeSupport: MultiItemTypeSupport<T>? = null

    constructor(context: Context, layoutId: Int, list: List<T>) {
        this.mContext = context
        this.mInflater = LayoutInflater.from(context)
        this.mLayoutId = layoutId
        this.mArrayList = list
    }

    constructor(context: Context, multiItemTypeSupport: MultiItemTypeSupport<T>, data: List<T>) {
        this.mContext = context
        this.mInflater = LayoutInflater.from(context)
        this.multiItemTypeSupport = multiItemTypeSupport
        this.mArrayList = data
    }

    override fun onBindViewHolder(holder: RecyclerViewXRefreshAdapter.CommonViewHolder, position: Int, payloads: List<Any>?) {
        super.onBindViewHolder(holder, position, payloads)
        if (position == mArrayList.size) return
        if (null == payloads || payloads.size < 1) {
            onBindViewHolder(holder, position, true)
        } else {
            for (payload in payloads) {
                // 刷新倒计时
                if (payload is Int && payload == UPDATE_COUNT_DOWN_TIME) {
                    updateClickItem(holder, position)
                }
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerViewXRefreshAdapter.CommonViewHolder, position: Int, isItem: Boolean) {
        val bean = mArrayList[position]
        holder.itemView.setOnClickListener(View.OnClickListener {
            if (onItemClickListener != null) {
                notifyItemRangeChanged(position, 1, UPDATE_COUNT_DOWN_TIME)
                onItemClickListener!!.onClick(bean)
            }
        })
        convert(holder, bean, position)
    }

    abstract fun convert(holder: CommonViewHolder, bean: T, position: Int)

    fun updateClickItem(holder: CommonViewHolder, position: Int) {}

    override fun getAdapterItemCount(): Int {
        return mArrayList.size
    }

    override fun getViewHolder(view: View): CommonViewHolder {
        return CommonViewHolder(view, false)
    }

    fun setData(list: List<T>) {
        this.mArrayList = list
        notifyDataSetChanged()
    }

    override fun getAdapterItemViewType(position: Int): Int {
        return if (multiItemTypeSupport != null) {
            multiItemTypeSupport!!.getItemViewType(position, mArrayList[position])
        } else {
            0
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int, isItem: Boolean): CommonViewHolder {
        val layoutId: Int
        if (multiItemTypeSupport != null) {
            layoutId = multiItemTypeSupport!!.getLayoutId(viewType)
        } else {
            layoutId = mLayoutId
        }
        val v = mInflater.inflate(layoutId, parent, false)
        return CommonViewHolder(v, true)
    }

    fun insert(bean: T, position: Int) {
        insert(mArrayList, bean, position)
    }

    fun remove(position: Int) {
        remove(mArrayList, position)
    }

    fun clear() {
        clear(mArrayList)
    }

    fun getItem(position: Int): T? {
        return if (position < mArrayList.size)
            mArrayList[position]
        else
            null
    }

    /** */
    interface OnItemClickListener<B> {
        fun onClick(bean: B)
    }
    /** */
    /**RecyclerView不同的Item类型支持接口 */
    interface MultiItemTypeSupport<T> {
        fun getLayoutId(itemType: Int): Int
        fun getItemViewType(position: Int, t: T): Int
    }


    /** */
    class CommonViewHolder(itemView: View, isItem: Boolean) : RecyclerView.ViewHolder(itemView) {

        private var mListView: SparseArray<View>? = null
        private var mConvertView: View? = null

        init {
            if (isItem) {
                this.mConvertView = itemView
                mListView = SparseArray()
            }
        }

        /**
         * 通过viewId获取控件
         */
        fun <T : View> getView(viewId: Int): T? {
            var v: View? = mListView!!.get(viewId)
            if (v == null) {
                v = mConvertView!!.findViewById(viewId)
                mListView!!.put(viewId, v)
            }
            return v as T?
        }
    }

    companion object {
        private val UPDATE_COUNT_DOWN_TIME = 1
    }
}